﻿using HardToolApp.Tool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;

namespace HardToolApp.Model
{
    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum SPIDataSize
    {
        [Description("数据长度:8")]
        SPI_DATA_SIZE_8 = 0,
        [Description("数据长度:16")]
        SPI_DATA_SIZE_16,
    };
    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum SPIFirstBit
    {
        [Description("高位在前")]
        SPI_MSB = 0,
        [Description("低位在前")]
        SPI_LSB = 1,
    };
    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum SPIBaud
    {
        [Description("18M")]
        SPI_BAUD_18M = 0,
        [Description("9M")]
        SPI_BAUD_9M,
        [Description("4500K")]
        SPI_BAUD_4500K,
        [Description("2250K")]
        SPI_BAUD_2250K,
        [Description("1125K")]
        SPI_BAUD_1125K,
        [Description("562.5K")]
        SPI_BAUD_562500,
        [Description("281.25K")]
        SPI_BAUD_281250,
    };
    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum SPI_CPOL
    {
        [Description("CLK空闲时低电平")]
        SPI_CPOL_LOW = 0,
        [Description("CLK空闲时高电平")]
        SPI_CPOL_HIGH,
    };
    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum SPI_CPHA
    {
        [Description("第一个边沿采样")]
        SPI_CPHA_1EDGE = 0,
        [Description("第二个边沿采样")]
        SPI_CPHA_2EDGE = 1,
    };

    internal class SPIMasterModel: Communicate
    {
        SPIDataSize _dataSize;
        public SPIDataSize dataSize { get { return _dataSize; } set { SetProperty(ref _dataSize, value); } }
        SPIFirstBit _firstBit;
        public SPIFirstBit firstBit { get { return _firstBit; } set { SetProperty(ref _firstBit, value); } }
        SPIBaud _baud;
        public SPIBaud baud { get { return _baud; } set { SetProperty(ref _baud, value); } }
        SPI_CPHA _CPHA;
        public SPI_CPHA CPHA { get { return _CPHA; } set { SetProperty(ref _CPHA, value); } }
        SPI_CPOL _CPOL;
        public SPI_CPOL CPOL { get { return _CPOL; } set { SetProperty(ref _CPOL, value); } }
        byte _readLen = 4;
        public byte readLen { get { return _readLen; } set { SetProperty(ref _readLen, value); } }
        byte _readWriteLen = 4;
        public byte readWriteLen { get { return _readWriteLen; } set { SetProperty(ref _readWriteLen, value); } }
        UInt32 _writeDelay = 3;
        public UInt32 writeDelay { get { return _writeDelay; } set { SetProperty(ref _writeDelay, value); } }

        bool? _dcLevel = false;
        public bool? dcLevel { get { return _dcLevel; } set { SetProperty(ref _dcLevel, value); } }

        public SPIMasterModel() : base(CommCmd.COMM_SPI_MASTER) { }

        public override string ToString()
        {
            switch (subCmd)
            {
                case 1:
                    StringBuilder sb = new StringBuilder();
                    sb.Append("改变SPI主机参数->");
                    sb.Append(EnumExtensions.GetDescription(dataSize)+',');
                    sb.Append(EnumExtensions.GetDescription(firstBit) + ',');
                    sb.Append(EnumExtensions.GetDescription(baud)+',');
                    sb.Append(EnumExtensions.GetDescription(CPOL) + ',');
                    sb.Append(EnumExtensions.GetDescription(CPHA) + '\n');
                    sb.Append("\n");
                    return sb.ToString();
                case 2:
                    return "SPI主机写入数据->" + sendBytes + "\n";
                case 3:
                    return "SPI主机读取"+readLen.ToString()+"个数据" + "\n";
                case 4:
                    return "SPI主机写入" + sendBytes + "后延时" + writeDelay.ToString() + "us 读取" + readWriteLen.ToString() + "个字节" + "\n";
                default:
                    return "未知命令" + "\n";
            }
        }
    }
}
